package homelesspartners.client.widgets;

import homelesspartners.client.Controller;
import homelesspartners.client.ServerFacade;
import homelesspartners.client.model.Gift;
import homelesspartners.client.model.Shelter;
import homelesspartners.client.model.Story;
import homelesspartners.client.model.User;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class StoriesWidget extends Composite implements ClickListener {

	private VerticalPanel verticalPanel = new VerticalPanel();
	
	private String headingText1 = "&nbsp;<div id='top'></div><b>STEP 3 - Personal Stories for ";
	private String headingText2 = "</b><br /><br />";
	
	private HTML heading = new HTML(headingText1 + "Selected Shelter" + headingText2);
	
	private HTML subText =
			new HTML(
					"If you would like to give any of the requested gifts below, please click the button beside it (gifts in <font color='red'>red</font> have already been pledged).<br /> <br />");

	private HTML subText2 =
			new HTML(
					"When you are done, click <b>FINISH</b> to see instructions on how to send the gifts or caring message.<br /> <br /> <br />");

	private HTML subText3 =
			new HTML(
					"Thank You and may God bless you for your interest in helping the homeless this holiday season.  Merry Christmas!<br /><br /> <br />");

	private PushButton previous = new PushButton("Previous " + Shelter.PAGE_SIZE, this);
	private PushButton next = new PushButton("Next " + Shelter.PAGE_SIZE, this);
	private HTML rule = new HTML("<hr />");
	private HTML storyCount = new HTML("Stories #");
	private HTML noMoreStories = new HTML("There are no more stories for this shelter.<br /><br />");
	private HTML noStories = new HTML("There are no stories available for this shelter.  We are in the process of interviewing the people at this shelter and gathering their stories before Christmas.  Please click on the <b>choose shelter</b> link above to try another shelter, or you can <a href='./pages/samplestories.html' target='_blank'>read sample stories from last year's program</a>.<br /><br />");
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	
	private FlexTable table = new FlexTable();

	private static StoriesWidget instance = new StoriesWidget();

	public static StoriesWidget getInstance() {
		return instance;
	}

	public StoriesWidget() {
		verticalPanel.add(heading);
		verticalPanel.add(subText);
		verticalPanel.add(subText2);
		verticalPanel.add(subText3);

		previous.setVisible(false);
		next.setVisible(false);
		storyCount.setVisible(false);

		Label spacer1 = new Label(" ");
		HTML spacer2 = new HTML("<br /><br />");

		storyCount.setWidth("300px");
		previous.setWidth("100px");
		next.setWidth("100px");
		spacer1.setWidth("10px");
		spacer2.setWidth("10px");

		buttonPanel.add(storyCount);
		buttonPanel.add(previous);
		buttonPanel.add(spacer1);
		buttonPanel.add(next);
		buttonPanel.add(spacer2);

		verticalPanel.add(table);
		table.setCellPadding(2);
		table.setBorderWidth(0);
		verticalPanel.add(rule);
		verticalPanel.add(buttonPanel);

		Image giftImage = new Image("./images/gift-icon.gif");
		verticalPanel.add(giftImage);
		giftImage.setVisible(false);
		
		initWidget(verticalPanel);
	}

	public void refresh(Shelter aShelter) {

		heading.setHTML(headingText1 + aShelter.getName() + headingText2);
		
		if(aShelter.isLoading()){
			verticalPanel.remove(noMoreStories);
			verticalPanel.remove(noStories);
			return;
		}
		
		verticalPanel.remove(table);
		verticalPanel.remove(rule);
		verticalPanel.remove(buttonPanel);

		table = new FlexTable();
		table.setCellPadding(2);
		table.setBorderWidth(0);
		
		verticalPanel.add(table);
		verticalPanel.add(rule);
		verticalPanel.add(buttonPanel);

		if (!aShelter.isPagePopulated()) {
			storyCount.setVisible(false);
			next.setVisible(false);
			if(aShelter.getPagedStories().size() == 0){
				verticalPanel.add(noStories);
				return;	
			}
			verticalPanel.add(noMoreStories);
			return;
		} 
		verticalPanel.remove(noMoreStories);
		verticalPanel.remove(noStories);
		
		storyCount.setVisible(false);
		previous.setVisible(false);
		next.setVisible(false);

		if (aShelter.hasMore()) {
			next.setVisible(true);
			storyCount.setVisible(true);
		}

		if (aShelter.hasPrevious()) {
			previous.setVisible(true);
			storyCount.setVisible(true);
		}

		if (previous.isVisible() && next.isVisible()) {
			storyCount.setWidth("300px");
		} else {
			storyCount.setWidth("400px");
		}

		if (storyCount.isVisible()) {
			int offsetStart = aShelter.getStoryOffset() + 1;
			int offsetEnd = offsetStart + Shelter.PAGE_SIZE;
			if (offsetEnd > aShelter.getPagedStories().size()) {
				offsetEnd = aShelter.getPagedStories().size();
			}
			String label = storyCount.getText();
			label = label.substring(0, label.indexOf('#') + 1);
			label = label + offsetStart + " - " + offsetEnd;
			storyCount.setHTML(label);

		}

		table.setBorderWidth(0);
		int row = 0;
		for (int x = 0; x < Shelter.PAGE_SIZE && x + aShelter.getStoryOffset() < aShelter.getPagedStories().size(); x++) {
			Story story = (Story) aShelter.getPagedStories().get(x + aShelter.getStoryOffset());
			table.getFlexCellFormatter().setColSpan(row, 0, 3);
			table.setWidget(row, 0, new HTML("<b>" + story.getHeaderText() + "</b>"));
			row++;
			table.getFlexCellFormatter().setColSpan(row, 0, 3);
			table.setWidget(row, 0, new Label(story.getStory()));
			row++;
			table.getFlexCellFormatter().setColSpan(row, 0, 3);
			table.setWidget(row, 0, new Label("For Christmas, " + story.getFirstName() + " would like:"));
			row++;

			table.getFlexCellFormatter().setWidth(row, 0, "50px");
			table.getFlexCellFormatter().setWidth(row, 1, "10px");
			table.getFlexCellFormatter().setWidth(row, 2, "420px");

			for (int y = 0; y < story.getGifts().length && story.getGifts()[y] != null; y++) {
				final Gift gift = story.getGifts()[y];
				final Label giftLabel = new Label(" - " + gift.getDescription());
				Image giftImage = new Image("./images/gift-icon.gif");
				final PushButton giftButton = new PushButton(giftImage);
				giftButton.addClickListener(new ClickListener() {

					public void onClick(Widget sender) {
						if (!User.getInstance().isLoggedIn()) {
							LoginWidget.prompt();
						} else {
							giftLabel.setStyleName("gift-desc-pledged");
							giftButton.setVisible(false);
							gift.setDonor("pledged");
							Controller.pledgeGift(gift);
						}
					}
				});
				giftButton.setWidth("40px");

				table.getFlexCellFormatter().setAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER,
						HasVerticalAlignment.ALIGN_TOP);
				table.setWidget(row, 0, new Label(" "));
				table.setWidget(row, 1, giftButton);
				table.setWidget(row, 2, giftLabel);

				if (gift.isPledged()) {
					giftLabel.setStyleName("gift-desc-pledged");
					giftButton.setVisible(false);
				}
				row++;

			}

			table.setWidget(row, 0, new Label(" "));
			row++;
		}

	}
		
		
	public void onClick(Widget sender) {

		DOM.scrollIntoView(DOM.getElementById("top"));
		
		if (sender == next) {
			Shelter shelter = User.getInstance().getCurrentShelter();
			shelter.goNext();
			if (shelter.isPagePopulated()) {
				refresh(shelter);
				return;
			} else {
				ServerFacade.loadPageOfStories(shelter, shelter.getStoryOffset());
			}
		} else if (sender == previous) {
			Shelter shelter = User.getInstance().getCurrentShelter();
			shelter.goPrevious();
			refresh(shelter);
		}

	}
}
